home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 September (Japanese) / CICA Shareware for Windows CD-ROM (Walnut Creek) (September 1995) (Japanese) (Disc 2).iso / disc2 / patches / symantec / rtlinc.exe / COMPLEX.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-19  |  10.6 KB  |  434 lines

  1. /*ident    "@(#)complex.h    1.3   6/2/90" */
  2.  
  3. #ifndef __COMPLEX_H
  4. #define __COMPLEX_H    1
  5.  
  6. /*
  7. **  (c) Copyright 1988-1990 by Dyad Software Corp.
  8. **  All Rights Reserved
  9. **
  10. **  Authors: lwd, geb
  11. */
  12.  
  13. #ifndef __MATH_H
  14. #include <math.h>
  15. #endif
  16.  
  17. #ifndef __FLTPNT_H
  18. #include <fltpnt.h>
  19. #endif
  20.  
  21. class ostream;
  22. class istream;
  23.  
  24.  
  25. class complex
  26. {
  27.  
  28.  public:
  29.    friend double real ( const complex& );     
  30.    friend double imag ( const complex& );     
  31.  
  32.    friend complex cos ( const complex& );
  33.    friend complex cosh ( const complex& );
  34.    friend complex sin ( const complex& );
  35.    friend complex sinh ( const complex& );
  36.    friend complex tan ( const complex& );
  37.    friend complex tanh ( const complex& );
  38.    friend complex log ( const complex& );
  39.    friend complex log10 ( const complex& );
  40.    friend complex sqrt ( const complex& );
  41.    friend double abs ( const complex& );
  42.    friend complex conj ( const complex& );
  43.    friend double norm ( const complex& );
  44.    friend double modulus ( const complex& );
  45.    friend double arg ( const complex& );
  46.    friend complex asin ( const complex& );
  47.    friend complex acos ( const complex& );
  48.    friend complex atan ( const complex& );
  49.    friend complex asinh ( const complex& );
  50.    friend complex atanh ( const complex& );
  51.    friend complex exp ( const complex& );
  52.    friend complex polar ( double r, double theta = 0);
  53.  
  54.    friend complex operator + ( const complex&, const complex& );
  55.    friend complex operator + ( double, const complex& );
  56.    friend complex operator + ( const complex&, double );
  57.  
  58.    friend complex operator - ( const complex&, const complex& );
  59.    friend complex operator - ( double, const complex& );
  60.    friend complex operator - ( const complex&, double );
  61.  
  62.    friend complex operator * ( const complex&, const complex& );
  63.    friend complex operator * ( double, const complex& );
  64.    friend complex operator * ( const complex&, double );
  65.  
  66.    friend complex operator / ( const complex&, const complex& );
  67.    friend complex operator / ( double, const complex& );
  68.    friend complex operator / ( const complex&, double );
  69.  
  70.    friend complex pow( const complex& lhs, double rhs );
  71.    friend complex pow( const complex& lhs, const complex & rhs );
  72.    friend complex pow( double lhs, const complex & rhs);
  73.    friend complex pow( const complex& lhs, int rhs);
  74.  
  75.    friend int operator && ( const complex&, const complex& );
  76.    friend int operator && ( double, const complex& );
  77.    friend int operator && ( const complex&, double );
  78.  
  79.    friend int operator || ( const complex&, const complex& );
  80.    friend int operator || ( double, const complex& );
  81.    friend int operator || ( const complex&, double );
  82.  
  83.    friend int operator != ( const complex&, const complex& );
  84.    friend int operator != ( double, const complex& );
  85.    friend int operator != ( const complex&, double );
  86.  
  87.    friend int operator == ( const complex&, const complex& );
  88.    friend int operator == ( double, const complex& );
  89.    friend int operator == ( const complex&, double );
  90.  
  91.    friend ostream& operator << ( ostream& s, const complex& x);
  92.    friend istream& operator >> ( istream& s, complex& x);
  93.  
  94.  public:
  95.  
  96.    complex ( );
  97.    complex ( double r, double i ); /*   this is not declared (double r, double i = 0)
  98.                                         to avoid implicit conversion. Binary operations
  99.                                         are explicitly declared thus avoiding potential 
  100.                                         ambiguities. */
  101.    complex ( const complex & );   
  102.    double& real ( );
  103.    double& imag ( );
  104.    
  105.    complex& operator = ( const complex& );
  106.    complex& operator = ( double );
  107.    
  108.    complex& operator += ( const complex& );
  109.    complex& operator += ( double );
  110.    
  111.    complex& operator -= ( const complex& );
  112.    complex& operator -= ( double );
  113.    
  114.    complex& operator *= ( const complex& );
  115.    complex& operator *= ( double );
  116.    
  117.    complex& operator /= ( const complex& );
  118.    complex& operator /= ( double );
  119.    
  120.    int operator ! () const;
  121.    complex operator - () const;
  122.    
  123.  private:
  124.    double re;
  125.    double im;
  126.  };
  127.  
  128. inline complex::complex ( )
  129.     re = im = NAN;
  130. }
  131.  
  132. inline complex::complex ( double r, double i ): re(r), im(i) {}
  133.      inline complex::complex( const complex& z)
  134. {
  135.   re = z.re;
  136.   im = z.im;
  137. }    
  138.  
  139. inline double real ( const complex& z){ return z.re; }     
  140. inline double imag ( const complex& z){ return z.im; }
  141.      
  142. inline double& complex::real (){ return re; }     
  143. inline double& complex::imag (){ return im; }
  144.      
  145. /* operator + */
  146. inline complex operator + ( const complex& lhs, const complex& rhs )
  147. {
  148.   return  complex ( lhs.re + rhs.re, lhs.im + rhs.im );
  149. }
  150. inline complex operator + ( double  lhs, const complex& rhs )
  151. {
  152.   return  complex ( lhs + rhs.re, rhs.im );
  153. }
  154. inline complex operator + ( const complex& lhs, double rhs )
  155. {
  156.   return  complex ( lhs.re + rhs, lhs.im );
  157. }
  158.  
  159. /* operator - */
  160. inline complex operator - ( const complex& lhs, const complex& rhs )
  161. {
  162.   return  complex ( lhs.re - rhs.re, lhs.im - rhs.im );
  163. }
  164. inline complex operator - ( double lhs, const complex& rhs )
  165. {
  166.   return  complex ( lhs - rhs.re, - rhs.im );
  167. }
  168. inline complex operator - ( const complex& lhs, double rhs )
  169. {
  170.   return  complex ( lhs.re - rhs, lhs.im );
  171. }
  172.  
  173. /* operator * */
  174. inline complex operator * ( const complex& lhs, const complex& rhs )
  175. {
  176.   return  complex ( lhs.re * rhs.re - lhs.im * rhs.im, 
  177.            lhs.re * rhs.im + lhs.im*rhs.re );
  178. }
  179. inline complex operator * ( double lhs, const complex& rhs )
  180. {
  181.   return  complex ( lhs * rhs.re, lhs * rhs.im );
  182. }
  183. inline complex operator * ( const complex& lhs, double rhs )
  184. {
  185.   return  complex ( lhs.re * rhs, lhs.im*rhs );
  186. }
  187.  
  188. /* operator / */
  189.  
  190. inline complex operator / ( const complex& lhs, double rhs )
  191. {
  192.   return complex( lhs.re / rhs, lhs.im /rhs );
  193. }
  194.  
  195. /* unary operators */
  196. inline complex complex::operator - ( ) const
  197. {
  198.   return  complex ( -re, -im );
  199. }
  200.  
  201. inline int complex::operator ! ( )  const
  202. {
  203.   return  ( re == 0 ) && ( im == 0 ) ;
  204. }    
  205.  
  206.  
  207. inline complex & complex::operator = ( const complex& z)
  208. {
  209.   re = z.re;
  210.   im = z.im;
  211.   return  *this;
  212. }    
  213.  
  214. inline complex & complex::operator = ( double x)
  215. {
  216.   re = x;
  217.   im = 0;
  218.   return  *this;
  219. }    
  220.  
  221. /* operator *= */
  222. inline complex & complex::operator *= ( const complex& z)
  223. {
  224.   *this = *this * z;
  225.   return  *this;
  226. }
  227. inline complex & complex::operator *= ( double x )
  228. {
  229.   re *= x;
  230.   im *= x;
  231.   return  *this;
  232. }
  233.  
  234. inline complex & complex::operator /= ( const complex& z)
  235. {
  236.   *this = *this / z;
  237.   return  *this;
  238. }    
  239. inline complex & complex::operator /= ( double x)
  240. {
  241.   re /= x;
  242.   im /= x;
  243.   return  *this;
  244. }    
  245.  
  246. inline complex & complex::operator += ( const complex& z)
  247. {
  248.   re += z.re;
  249.   im += z.im;
  250.   return  *this;
  251. }
  252. inline complex & complex::operator += ( double x)
  253. {
  254.   re += x;
  255.   return  *this;
  256. }
  257.  
  258. inline complex & complex::operator -= ( const complex& z)
  259. {
  260.   re -= z.re;
  261.   im -= z.im;
  262.   return  *this;
  263. }
  264.  
  265. inline complex & complex::operator -= ( double x)
  266. {
  267.   re -= x;
  268.   return  *this;
  269. }    
  270.  
  271. /* operator && */
  272. inline int operator && ( const complex & lhs, const complex & rhs )
  273. {
  274.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) &&
  275.     (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  276. }    
  277. inline int operator && ( double lhs, const complex & rhs )
  278. {
  279.   return  ( lhs != 0 ) && (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  280. }    
  281. inline int operator && ( const complex & lhs, double rhs )
  282. {
  283.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) && ( rhs != 0 ) ;
  284. }    
  285.  
  286. /* operator || */
  287. inline int operator || ( const complex & lhs, const complex & rhs )
  288. {
  289.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) ||
  290.     (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  291. }
  292. inline int operator || ( double lhs, const complex & rhs )
  293. {
  294.   return  ( lhs != 0 ) || (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  295. }
  296. inline int operator || ( const complex & lhs, double rhs )
  297. {
  298.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) || ( rhs != 0 );
  299. }    
  300.  
  301. /* operator != */
  302. inline  int operator != ( const complex& lhs, const complex& rhs )
  303. {
  304.   return  ( lhs.re != rhs.re ) || ( lhs.im != rhs.im );
  305. }
  306. inline  int operator != ( double lhs, const complex& rhs )
  307. {
  308.   return  ( lhs != rhs.re ) || ( 0 != rhs.im );
  309. }
  310. inline  int operator != ( const complex& lhs, double rhs )
  311. {
  312.   return  ( lhs.re != rhs ) || ( lhs.im != 0 );
  313. }
  314.  
  315. /* operator == */
  316. inline  int operator == ( const complex& lhs, const complex& rhs )
  317. {
  318.   return  ( lhs.re == rhs.re ) && ( lhs.im == rhs.im );
  319. }
  320. inline  int operator == ( double lhs, const complex& rhs )
  321. {
  322.   return  ( lhs == rhs.re ) && ( 0 == rhs.im );
  323. }
  324. inline  int operator == ( const complex& lhs, double rhs )
  325. {
  326.   return  ( lhs.re == rhs ) && ( lhs.im == 0 );
  327. }
  328.  
  329. inline double arg( const complex& z)
  330. {
  331.   return atan2(z.im,z.re);
  332. }
  333.  
  334. inline complex atanh( const complex& z) 
  335. {
  336.   complex iz(-z.im, z.re);
  337.   complex ix = atan(iz);
  338.   return complex ( ix.im, -ix.re);
  339. }
  340.  
  341. inline complex asinh( const complex& z)
  342. {
  343.   complex iz(-z.im, z.re);
  344.   complex ix = asin(iz);
  345.   return complex ( ix.im, -ix.re);
  346. }
  347.  
  348. inline double norm( const complex& z)
  349. {
  350.   return z.re*z.re + z.im*z.im;
  351. }
  352.  
  353. inline double modulus( const complex& z)
  354. {
  355.   return abs(z);
  356. }
  357.  
  358. inline complex conj( const complex& z)
  359. {
  360.   return complex( z.re, -z.im );
  361. }
  362.  
  363. inline complex cos( const complex& z )
  364. {
  365.   return complex( cos(z.re) * cosh( z.im ),  -( sin( z.re) * sinh(z.im)));
  366. }
  367.  
  368. inline complex cosh( const complex& z )
  369. {
  370.   return complex ( cosh(z.re) * cos(z.im), sinh(z.re) * sin(z.im) );
  371. }
  372.  
  373. inline complex sin( const complex& z )
  374. {
  375.   return complex ( sin(z.re) * cosh(z.im), cos(z.re) * sinh(z.im) );
  376. }
  377.  
  378. inline complex sinh( const complex& z )
  379. {
  380.   return complex ( sinh(z.re) * cos(z.im), cosh(z.re) * sin(z.im) );
  381. }
  382.  
  383. inline complex tan( const complex& z )
  384. {
  385.   double x = 2*z.re;
  386.   double y = 2*z.im;
  387.   double t = 1.0/(cos(x) +cosh(y));
  388.   
  389.   return complex( t*sin(x), t*sinh(y) );
  390. }
  391.  
  392. inline complex tanh( const complex& z )
  393. {
  394.   double x = 2*z.re;
  395.   double y = 2*z.im;
  396.   double t = 1.0/(cosh(x) +cos(y));
  397.   
  398.   return complex( t*sinh(x), t*sin(y) );
  399. }    
  400.  
  401. inline complex exp( const complex& z )
  402. {
  403.   double x = exp(z.re);
  404.   return complex( x*cos(z.im), x*sin(z.im) );
  405. }    
  406.  
  407. inline complex log( const complex& z )
  408. {
  409.   return complex( log( abs(z) ), arg( z ) );
  410. }    
  411.  
  412. inline complex log10( const complex& z )
  413. {
  414.   return complex( 0.2171472409516259*log( norm(z) ), arg( z ) );
  415. }    
  416.  
  417. inline complex polar ( double r, double theta )
  418. {
  419.   return complex ( r * cos(theta), r * sin(theta));
  420. }
  421.  
  422. #endif
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.